import java.util.Properties;

public class ArgumentProcessor {

	public static boolean process(Properties theProps, String[] args) {
		
		final String width = "800";
		final String height = "600";
		final String fullscreen = "false";
		final String local = "false";
		final String verbose = "false";
		final String row = "3";
		final String column = "3";
		final String offset = "   ";
				
		String eol = System.getProperty("line.separator"); 
		StringBuilder usage = new StringBuilder();
		usage.append("Usage:" + eol);
		usage.append("-? OR --help" + eol);
		usage.append(offset + "Print this usage information." + eol);
		usage.append("-f OR --fullscreen" + eol);
		usage.append(offset + "Set fullscreen mode. (default is " + fullscreen + ")" + eol);
		usage.append("-w X OR --width X" + eol);
		usage.append(offset + "Set the width. (default is " + width + ")" + eol);
		usage.append("-h X OR --height X" + eol);
		usage.append(offset + "Set the height. (default is " + height + ")" + eol);
		usage.append("-r X OR --row X" + eol);
		usage.append(offset + "Set the number of rows in the grid. (default is " + row + ")" + eol);
		usage.append("-c X OR --column X" + eol);
		usage.append(offset + "Set the number of columns in the grid. (default is " + column + ")" + eol);
		usage.append("-l OR --local" + eol);
		usage.append(offset + "Set processing as local. (default is " + local + ")" + eol);
		usage.append("-v OR --verbose" + eol);
		usage.append(offset + "Turn on verbose mode (default is " + verbose + ")" + eol);
		usage.append(eol + offset + "Press ESC to quit!");
		
		String printMeUsage = usage.toString();
		
		
		//Set the Defaults
		theProps.setProperty("fullscreen", fullscreen);
		theProps.setProperty("width", width);
		theProps.setProperty("height", height);
		theProps.setProperty("local", local);
		theProps.setProperty("verbose", verbose);
		theProps.setProperty("row", row);
		theProps.setProperty("column", column);
		
		//Process the args, reconfigure the properties
		for(int i=0; i<args.length; i++){
			String arg = args[i];
			if(arg.equals("-?") || arg.equals("--help")){
				System.out.println(printMeUsage);
				return false;
			}
			if(arg.equals("-f") || arg.equals("--fullscreen")){
				theProps.setProperty("fullscreen", "true");
			}
			else if(arg.equals("-h") || arg.equals("--height")){
				i++;				
				//is there a next argument?
				if(i >= args.length){
					System.out.println(printMeUsage);
					return false;
				}					
				String nextArg = args[i];				
				if(checkIfPositiveNumber(nextArg) == false){
					System.out.println(printMeUsage);
					return false;
				}
				theProps.setProperty("height", nextArg);		
			}
			else if(arg.equals("-w") || arg.equals("--width")){
				i++;				
				//is there a next argument?
				if(i >= args.length){
					System.out.println(printMeUsage);
					return false;
				}					
				String nextArg = args[i];				
				if(checkIfPositiveNumber(nextArg) == false){
					System.out.println(printMeUsage);
					return false;
				}
				theProps.setProperty("width", nextArg);				
			}
			else if(arg.equals("-r") || arg.equals("--row")){
				i++;				
				//is there a next argument?
				if(i >= args.length){
					System.out.println(printMeUsage);
					return false;
				}					
				String nextArg = args[i];				
				if(checkIfPositiveNumber(nextArg) == false){
					System.out.println(printMeUsage);
					return false;
				}
				theProps.setProperty("row", nextArg);				
			}
			else if(arg.equals("-c") || arg.equals("--column")){
				i++;				
				//is there a next argument?
				if(i >= args.length){
					System.out.println(printMeUsage);
					return false;
				}					
				String nextArg = args[i];				
				if(checkIfPositiveNumber(nextArg) == false){
					System.out.println(printMeUsage);
					return false;
				}
				theProps.setProperty("column", nextArg);				
			}
			else if(arg.equals("-l") || arg.equals("--local")){
				theProps.setProperty("local", "true");
			}
			else if(arg.equals("-v") || arg.equals("--verbose")){
				theProps.setProperty("verbose", "true");
			}
		}
		return true;		
	}
	
	private static boolean checkIfPositiveNumber(String in){
		//is the next argument a positive number?
		try{
			int theheight = Integer.parseInt(in);
			if(theheight <= 0){
				return false;
			}
		}
		catch (NumberFormatException x){
			return false;
		}
		return true;
		
	}
	

}
